/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import Entities.User;
import java.io.*;
import java.net.*;

import javax.servlet.*;
import javax.servlet.http.*;


import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;

/**
 *
 * @author JackOddie
 */
public class ControllerServlet extends HttpServlet {

    @Override
    public void service(ServletRequest request, ServletResponse response)
            throws ServletException, java.io.IOException {
        
        String id = request.getParameter("id");
        String passWd = request.getParameter("passWd");
        
        

        if (id == null || passWd == null) {
            String url = "index.jsp";
            String action = request.getParameter("action");

            if (action != null) {
                if (action.equals("date")) {
                    url = "addForm.jsp";
                } else if (action.equals("logout")) {
                    url = "Logout.jsp";
                } else if (action.equals("login")) {
                    url = "Login.jsp";
                } else if (action.equals("search")) {
                    url = "Search.jsp";
                } else if (action.equals("userDetail")) {
                    url = "UserDetails.jsp";
                }
            } 
            RequestDispatcher rd = request.getRequestDispatcher(url);
            rd.forward(request, response);
        } else {
            try {


                if (User.isExist(id, passWd)) {
                    // login sukses

                    RequestDispatcher rd = request.getRequestDispatcher("MenuUser.jsp");
                    rd.forward(request, response);
                } else {
                    // login gagal
                    request.setAttribute("error", "yes");
                    RequestDispatcher rd = request.getRequestDispatcher("index.jsp");
                    rd.forward(request, response);
                }
            } catch (SQLException ex) {
                Logger.getLogger(ControllerServlet.class.getName()).log(Level.SEVERE, null, ex);
            }

        }


    }
    }


